<?php

namespace App\Http\Controllers\Admin;

use App\Product_comment;
use App\Product_order;
use App\Product_order_pro;
use App\Realname;
use App\Shop;
use App\ShopComments;
use App\Logic\OrderLogic;

class CloseorderController extends AdminController
{
    /**
     * 取消没有支付的订单
     */
    public  static function closeOrder(){
        $time = '30 Minute';
        $date = date('Y-m-d H:i:s', strtotime(date("Y-m-d H:i:s") ." - " .$time ));
        echo $date."<br>";
        $orders = Product_order::where('status',1)->where('created_at','<', $date)->get();
        foreach ($orders as $order){
            $order->status = 6;
            if($order->save()){
                echo $order->id."<br>";
                //反-刷库存
                OrderLogic::changeStockAndSale($order->order_id,true);
            }
        }
        echo "complate";
    }
    
    /**
     * 没有评论的单子自动评论
     */
    public static function commentsOrder(){
        //deliver_time 45天之后的
        $orders = Product_order::where('status',4)->where('deliver_time','<', time() - 45 * 24* 60*60)->get();
        foreach ($orders as $order){
            $order->status = 5;
            
            if($order->save()){
                echo $order->id."<br>";
                $real = Realname::where('member_id', $order->buyer_id)->first();
                $orderPros =  Product_order_pro::where('order_id', $order->order_id)->get();
                foreach ($orderPros as $order_pro){ 
                    $comments = new Product_comment();
                    $comments->shop_id = $order->member_id;
                    $comments->product_id = $order_pro->pid;
                    $comments->product_cat_id = $order_pro->pcatid;
                    $comments->product_img = $order_pro->pic;
                    $comments->name = $order_pro->name . ' ' . $order_pro->spec_value;
                    $comments->sku = $order_pro->spec_value;
                    $comments->star = 5;
                    $comments->content = '系统默认好评！';
                    $comments->images = '';
                    $comments->user_name = substr($real->member_name, 0,9).'****' ;
                    $comments->user_phone = $real->phone;
                    $comments->order_id = $order->order_id;
                    $comments->order_detail_id = $order_pro->id;
                    $comments->is_show = 1;
                    $comments->is_gold = 1;
                    $comments->is_top = 1;
                    $comments->is_default = 2;
                    $comments->hasimg = 2;
                    $comments->save();
                      
                    //更新商铺统计
                    $shopComments = ShopComments::where('shop_id', $order->member_id)->first();
                    if(!$shopComments){
                        $shop = Shop::where('member_id', $order->member_id)->first();
                        $shopComments = new ShopComments();
                        $shopComments->shop_id = $order->member_id;
                        $shopComments->company = $shop->company;
                        $shopComments->total_comments = 0;
                        $shopComments->auto_total_comments = 0;
                        $shopComments->good_comments = 0;
                        $shopComments->normal_comments = 0;
                        $shopComments->bad_comments = 0;
                        $shopComments->save();
                    }
                    $shopComments->total_comments += 1;
//                     $shopComments->auto_total_comments += 1;
                    $shopComments->good_comments += 1;
                    $shopComments->save();
                }
            }
        }
    }
    
    
}